//
//  SQLDataManager.m
//  BTRIP
//
//  Created by Rain on 15/11/24.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "SQLDataManager.h"
#import <sqlite3.h>
#import "FoodShopCollectModel.h"
#import "DetailGoodShopModel.h"
#import "CoreDataManager.h"

#define nameOfFile @"food.sqlite"
#define tableOfSQL @"myFood"

@interface SQLDataManager ()

@property (nonatomic, retain) CoreDataManager *coreDataManager;

@end

@implementation SQLDataManager

- (instancetype)init{
    self = [super init];
    if (self) {
        self.coreDataManager = [CoreDataManager shareManager];
    }
    return self;
}

+ (instancetype)shareSQLDataManager {
    static SQLDataManager *dataManager = nil;
    if (dataManager == nil) {
        dataManager = [[SQLDataManager alloc] init];
    }
    return dataManager;
}

static sqlite3 *db;

- (void)open {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *sqlPath = [documentPath stringByAppendingPathComponent:nameOfFile];
    
    NSLog(@"%@", sqlPath);
    
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    int result = sqlite3_open(sqlPath.UTF8String, &db);
    NSLog(@"%d", result);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    } else {
        NSLog(@"数据库打开失败:%d", result);
    }
}

- (void)createTable {
    NSString *createSQL = [NSString stringWithFormat:@"create table if not exists %@(title text PRIMARY KEY, image_url text, theUrl text)", tableOfSQL];
    sqlite3_exec(db, createSQL.UTF8String, NULL, NULL, nil);
}

- (void)insertInfo:(DetailGoodShopModel *)shopModel andTheID:(NSString *)theID {
    NSString *insert = [NSString stringWithFormat:@"insert into %@(title, image_url, theUrl) values('%@', '%@', '%@')", tableOfSQL, shopModel.name, shopModel.url, theID];
    int result = sqlite3_exec(db, insert.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"收藏成功");
    } else {
        NSLog(@"收藏失败：%d", result);
    }
    
}

- (void)close {
    sqlite3_close(db);
    db = nil;
}

- (NSArray *)searchTitle {
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *search = [NSString stringWithFormat:@"select * from '%@'", tableOfSQL];
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, search.UTF8String, -1, &stmt, nil);
    
    if (result == 0) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *image_url = sqlite3_column_text(stmt, 1);
            const unsigned char *theID = sqlite3_column_text(stmt, 2);
            
            // 创建model
            FoodShopCollectModel *model = [[FoodShopCollectModel alloc] init];
            model.name = [NSString stringWithUTF8String:(const char *)title];
            model.image_url = [NSString stringWithUTF8String:(const char *)image_url];
            model.theUrl = [NSString stringWithUTF8String:(const char *)theID];
            [array addObject:model];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    return array;
}

- (NSArray *)searchTitleStr {
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *search = [NSString stringWithFormat:@"select * from '%@'", tableOfSQL];
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, search.UTF8String, -1, &stmt, nil);
    
    if (result == 0) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)name];
            [array addObject:titleStr];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    return array;
}

- (void)deleteInfo:(NSString *)title {
    NSString *delete = [NSString stringWithFormat:@"delete from %@ where title = '%@'", tableOfSQL, title];
    int result = sqlite3_exec(db, delete.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败: %d", result);
    }
    
}

@end
